package com.h2.ref.server.user.relationship;

import java.util.ArrayList;
import java.util.Collection;

import com.h2.ref.server.user.IUserService;
import com.h2.ref.server.user.model.User;
import com.h2.ref.server.user.relationship.dao.UserRelationshipDaoInMem;

public class UserRelationshipService implements IUserRelationshipService {
   
   private UserRelationshipDaoInMem _userRelationshipDao;
   
   ////
   
   private IUserService _userService;
   
   ////
   ////

   @Override
   public Boolean create(UserRelationship relationship) {
      return getUserRelationshipDao().create(relationship);
   }

   @Override
   public Collection<UserRelationship> find(UserRelationship relationship) {
      return getUserRelationshipDao().find(relationship);
   }

   @Override
   public Collection<User> findUsers(UserRelationship relationship) {
      Collection<UserRelationship> relationships = find(relationship);
      Collection<User> results = new ArrayList<User>(relationships.size());
      User user;
      for (UserRelationship userRelationship : relationships) {
         user = getUserService().find(
               new User(userRelationship.getTargetId()));
         
         results.add(user);
      }
      return results;
   }

   @Override
   public Boolean update(UserRelationship relationship) {
      return getUserRelationshipDao().update(relationship);
   }

   @Override
   public Boolean delete(UserRelationship relationship) {
      return getUserRelationshipDao().delete(relationship);
   }

   /**
    * @return the userRelationshipDao
    */
   public UserRelationshipDaoInMem getUserRelationshipDao() {
      return _userRelationshipDao;
   }

   /**
    * @param userRelationshipDao the userRelationshipDao to set
    */
   public void setUserRelationshipDao(UserRelationshipDaoInMem userRelationshipDao) {
      _userRelationshipDao = userRelationshipDao;
   }

   /**
    * @return the userService
    */
   public IUserService getUserService() {
      return _userService;
   }

   /**
    * @param userService the userService to set
    */
   public void setUserService(IUserService userService) {
      _userService = userService;
   }

}
